package com.comsumer.api;
import com.comsumer.config.MQConfig;
import com.comsumer.config.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

@RestController
@RequestMapping("/api/addGood")
public class AddGood {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;
    private static final String REST_URL_PREFIX="http://producer/good";
    private static final String FACTORY_NUMBER="00001";
    @GetMapping("/addNums")
    public Result addNums(){
        String UUID= String.valueOf(System.currentTimeMillis())+FACTORY_NUMBER+(new Random(1000)).nextInt();
        rabbitTemplate.convertAndSend(MQConfig.EXCHANGE_NAME,
                MQConfig.ROUTINGKEY,UUID);
        return Result.success();
    }
    @GetMapping("/getNums")
    public Result getNums(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/getNums",Result.class);
    }
}
